﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Drawing;
using Reborn_WorldServer.App1AppHandler.Spells.Interfaces;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_DamageAbsorb : Buff, IDamageAbsorbBuff, IInstantCast
    {
        private object lockObj = new object();

        private int _AmmountToAbsorb = 300;

        public int BuffPriority() { return 0; }
        public int AbsorbDamageReturnRemainder (int incomingMeleeDamage)
        {
            lock (lockObj)
            {
                if (incomingMeleeDamage >= _AmmountToAbsorb)
                {
                    this.RemoveSpell(true, true);
                    return incomingMeleeDamage - _AmmountToAbsorb;
                }
                else
                {
                    _AmmountToAbsorb = _AmmountToAbsorb - incomingMeleeDamage;
                    return 0;
                }
            }
        }

        public override bool castWhileAlone(SpellIcon sIcon)
        {
            return true;
        }

        public override bool hasCustomTarget(SpellIcon sIcon)
        {
            if (sIcon.Skill == "Holy")
                return true;

            return false;
        }

        private List<Point> hiddenList;
        private CreatureBase choosenTarget;

        public override bool hasValidTarget(SpellIcon sIcon, Creature creature, List<Point> hiddenList)
        {
            this.hiddenList = hiddenList;

            List<CreatureBase> targetCreatures = new List<CreatureBase>();
            foreach (MapItem mapitem in creature.currentMapItem.mapItemsWithin4)
            {
                foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                {
                    if (creatureBase.Faction == creature.Faction && creatureBase.HP < creatureBase.MaxHP && creatureBase.buffManager.GetBuffByStackType(sIcon.StackType) == null)
                        targetCreatures.Add(creatureBase);
                }
            }

            int damagedAmount = 0;
            foreach (CreatureBase cb in targetCreatures)
            {
                if (CreatureAI.isHidden(cb.MapX, cb.MapY, hiddenList) == false)
                {
                    int damageValue = cb.MaxHP - cb.HP;
                    if (choosenTarget == null || damageValue > damagedAmount)
                    {
                        choosenTarget = cb;
                        damagedAmount = damageValue;
                    }
                }
            }

            if (choosenTarget == null)
                return false;
            else
                return true;
        }

        public Spell_DamageAbsorb()
        {
            minutesLeft = 10;
        }

        public override int GetOptionalValue()
        {
            return this._AmmountToAbsorb;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
            
            
            _AmmountToAbsorb = optionalValue;

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            //If its a creature custom cast, replace the targets ID
            if (choosenTarget != null)
                CreatureGUID = choosenTarget.CreatureID;

            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            _AmmountToAbsorb = SkillManager.AdjustForCombat(CastLevel, _AmmountToAbsorb, CastingCreatureWis);

            TargetCreatureBase.buffManager.UpdateBuffs();

            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
            }

            //Start Spell Timer
            spellTimer.Start();
        }

        public void InstantCast(CreatureBase castingCreature, CreatureBase targetCreatureBase, SpellIcon _spellIcon, int castLevel)
        {
            //If its a creature custom cast, replace the targets ID
            if (choosenTarget != null)
                targetCreatureBase = choosenTarget;

            if (base.InitializeInstantCast(castingCreature, targetCreatureBase, _spellIcon, castLevel, false) == false)
                return;


            _AmmountToAbsorb = SkillManager.AdjustForCombat(CastingCreatureLevel, _AmmountToAbsorb, CastingCreatureWis);

            TargetCreatureBase.buffManager.UpdateBuffs();

            foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin8)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
            }

            //Start Spell Timer
            spellTimer.Start();
        }
    }
}
